<?php
class App_User_Form_AdminUserEdit extends Main_Form
{
	/**
	 * @var App_User_Service_Role
	 */
	protected $_serviceRole;
	
	/**
     * @var array
     */
    protected $_decoratorsForm  = array(
        'PrepareElements',
        array('ViewScript', array('viewScript' => '_form/admin-user-edit.phtml')),
    );
	
	/**
	 * @return void
	 */
	public function init()
	{
		parent::init();
		
		$this->_serviceRole = new App_User_Service_Role();
		
		$this->setMethod('post');
		$this->setDecorators($this->_decoratorsForm);
		
		$this->addElement('hidden', 'old_login', array(
			'order'	=> 3,
			'decorators' => $this->_decoratorsHidden
		));

		$this->addElement('select', 'role_id', array(
			'label' => 'Роль',
			'multioptions' => $this->_serviceRole->getAllIdAndName(),
			'order'	=> 5,
		));
		
		$this->addElement('text', 'login', array(
			'label'	=> 'Логин',
			'required' => true,
			'validators' => array(
				array('validator' => 'StringLength', 'options' => array(2, 100)),
				array(
					'validator' => 'UniqueItem', 'options' => array (
                    	'App_User_Service_User', 'old_login', null,
                        'Пользователь с таким логином уже существует' )
                )
			),
			'order'	=> 10,
		));
		
		$this->addElement('password', 'password', array(
			'label'	=> 'Пароль',
			'validators' => array(
				array('validator' => 'StringLength', 'options' => array(3, 100)),
				array(
					'validator' => 'EqualInputs', 'options' => array (
                    	'password2', 'Пароли не совпадают' 
					)
                )
			),
			'order'	=> 20,
		));
		
		$this->addElement('password', 'password2', array(
			'label'	=> 'Подтверждение пароля',
			'validators' => array(
				array('validator' => 'StringLength', 'options' => array(3, 100)),
				array(
					'validator' => 'EqualInputs', 'options' => array (
                    	'password', 'Пароли не совпадают' 
					)
                )
			),
			'order'	=> 30,
		));
		
		$this->addElement('text', 'email', array(
			'label'	=> 'Почта',
			'required' => true,
			'validators' => array(
				array('validator' => 'StringLength', 'options' => array(3, 255)),
				array('validator' => 'EmailAddress')
			),
			'order'	=> 40,
		));
		
		$this->addElement('text', 'first_name', array(
			'label'	=> 'Имя',
			'required' => false,
			'order'	=> 70,
		));
		
		$this->addElement('text', 'second_name', array(
			'label'	=> 'Фамилия',
			'required' => false,
			'order'	=> 80,
		));
		
		$this->addElement('text', 'middle_name', array(
			'label'	=> 'Отчество',
			'required' => false,
			'order'	=> 90,
		));	
		
		$this->addElement('checkbox', 'f_active', array(
			'label'	=> 'Статус',
			'order'	=> 111,
		));
		
		$this->addElement('submit', 'addButton', array(
			'label' => 'Сохранить',
			'order'	=> 1000,
		));
	}
	
	/**
	 * @param array $data
     * @return Main_Form
     */
    public function setFormValues($data)
    {
		$data['old_login'] = $data['login'];
		$data['password'] = '';
    	return parent::setFormValues($data);
    }
    
	/**
     * @param  string $action 
     */
    public function setupByAction($action)
    {
        if ($action == 'add') {
        	$this->getElement('password')->setRequired(true);
        	$this->getElement('password2')->setRequired(true);
        }
    }

}